package helpers;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class UrlHepler {
	public static ArrayList<String> urlToParam(String str) {
		ArrayList<String> params = new ArrayList<String>();
		
		String regex = "^[^-]+[-][^-_]+";
		String str1 = StringHelper.findString(regex, str).get(0);
		str = str.replaceAll(regex + "[-]?", "");
		
		regex = "[^-_]*([-][^-_]*)*[^_]?";
		String str2 = StringHelper.findString(regex, str).get(0);
		if (!str2.isEmpty()) {
			str = str.replaceAll("[^-_]+([-][^-_]+)+[_]", "");
		} else {
			str = str.replaceAll("[_]?", "");
		}
		
		regex = "^[^-_]*[-]?[^-]*";
		String str3 = StringHelper.findString(regex, str).get(0);
		if (!str3.isEmpty()) {
			str = str.replaceAll("^[^-_]+[-][^-]+[-]?", "");
		}
		
		params.add(str1);
		params.add(str2);
		params.add(str3);
		params.add(str);
		return params;
	}
	
	public static String createURL(String str, int pos, HttpServletRequest request) {
		HttpSession session = request.getSession(true);
		String typeService = (String) session.getValue("typeService");
		String typeRealEstate = (String) session.getValue("typeRealEstate");
		String province = (String) session.getValue("province");
		
		String url = StringHelper.getAlias(typeService);
		if (pos == 2) {
			url += "-" + StringHelper.getAlias(str);
		} else if (pos > 2 && typeRealEstate != null) {
			url += "-" + StringHelper.getAlias(typeRealEstate);
		}
		
		if (pos == 3) {
			url += "_" + StringHelper.getAlias(str);
		} else if (pos > 3 && province != null) {
			url += "_" + StringHelper.getAlias(province);
		}
		
		if (pos == 4) {
			url += "-" + StringHelper.getAlias(str);
		}
		
		return url;
	}
	
	public static String getSiteName(String url) {
		String regex = "[^http://][^/]+";
		String str = StringHelper.findString(regex, url).get(0);
		return str;
	}
	
	public static void main(String[] args) {
		/*
		String str = "can-thue-cua-hang-ki-ot_ha-noi-ba-dinh";
		
		ArrayList<String> params = new ArrayList<String>();
		params = UrlHepler.urlToParam(str);
		
		System.out.println("String 1: " + params.get(0));
		System.out.println("String 2: " + params.get(1));
		System.out.println("String 3: " + params.get(2));
		System.out.println("String 4: " + params.get(3));*/
		
		/*getSiteName("http://www.nik.vn/site/search/2/?query=tim+nha+cau+giay");*/
	}
}
